/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import tpa.TablaPeriodica.Clasificacion;

/**
 *
 * @author leandro
 */
public class AgregarClasificacionAction extends TablaPeriodicaBaseAction
{
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        try
        {
            AgregarClasificacionActionForm actionForm = (AgregarClasificacionActionForm) form;
            actionForm.setStatus("");

            if(actionForm.getClasificacionId() < 0L)
            {
                Clasificacion clasificacion = new Clasificacion(actionForm.getNombre());
                getTablaPeriodicaSessionBean().AgregarClasificacion(clasificacion);
                actionForm.setStatus("Clasificación " + clasificacion.toString() + " agregada correctamente.");
            }
            else
            {
                Clasificacion clasificacion = getTablaPeriodicaSessionBean().ObtenerClasificacion(actionForm.getClasificacionId());
                clasificacion.setNombre(actionForm.getNombre());
                getTablaPeriodicaSessionBean().ActualizarClasificacion(clasificacion);
                actionForm.setStatus("Clasificación " + clasificacion.toString() + " actualizada correctamente.");
            }

            return mapping.findForward(SUCCESS);
        }
        catch (Exception exception)
        {
            ActionErrors errors = new ActionErrors();

            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.exception", exception.getMessage()));

            saveErrors(request, errors);
            return mapping.findForward(ERROR);
        }
    }
}
